﻿using System;
using Couchbase;
using Enyim.Caching.Memcached;
using Newtonsoft.Json;

namespace Century.EnterpriseLibrary.Caching
{
    public class CouchbaseDB : ICacheManager
    {
        public static readonly CouchbaseDB Instance = new CouchbaseDB();

        private readonly CouchbaseClient couchClient = new CouchbaseClient();

        private CouchbaseDB()
        {
           
        }
        public object Get<T>(string key, string region)
        {
            if (key.Length > 150)
            {
                key = Encrypt.ToMD5(key);
            }

            return couchClient.Get<T>((key + region).Replace(" ", "").ToLower());
        }
        public object Get<T>(string key)
        {
            if (key.Length > 150)
            {
                key = Encrypt.ToMD5(key);
            }

            return couchClient.Get<T>(key.Replace(" ", "").ToLower());
        }

        public void Set(string key, object value, string region, TimeSpan expiration)
        {
            if (value == null)
            {
                return;
            }
            if (key.Length > 150)
            {
                key = Encrypt.ToMD5(key);
            }
            string serializedObj = JsonConvert.SerializeObject(value, Formatting.Indented,
                                new JsonSerializerSettings()
                                {
                                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                                });
            couchClient.Store(StoreMode.Set, (key + region).Replace(" ", "").ToLower(), serializedObj, expiration);
        }

        public void Set(string key, object value, string region)
        {
            if (value == null)
            {
                return;
            }
            if (key.Length > 150)
            {
                key = Encrypt.ToMD5(key);
            }
            string serializedObj = JsonConvert.SerializeObject(value, Formatting.Indented,
                              new JsonSerializerSettings()
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                              });
            couchClient.Store(StoreMode.Set, (key + region).Replace(" ", "").ToLower(), serializedObj);
        }

        public void Set(string key, object value, TimeSpan expiration)
        {
            if (value == null)
            {
                return;
            }
            if (key.Length > 150)
            {
                key = Encrypt.ToMD5(key);
            }
            string serializedObj = JsonConvert.SerializeObject(value, Formatting.Indented,
                             new JsonSerializerSettings()
                             {
                                 ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                             });
            couchClient.Store(StoreMode.Set, key.Replace(" ", "").ToLower(), serializedObj, expiration);

        }

        public void Set(string key, object value)
        {
            if (value == null)
            {
                return;
            }
            if (key.Length > 150)
            {
                key = Encrypt.ToMD5(key);
            }
            string serializedObj = JsonConvert.SerializeObject(value, Formatting.Indented,
                             new JsonSerializerSettings()
                             {
                                 ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                             });
            couchClient.Store(StoreMode.Set, key.Replace(" ", "").ToLower(), serializedObj);
        }
        public void Remove(string key)
        {
            if (key.Length > 150)
            {
                key = Encrypt.ToMD5(key);
            }
            couchClient.Remove(key.Replace(" ", "").ToLower());
        }


    }
}
